package cn.ylz.ylmall.account.controller;

import cn.ylz.ylmall.account.dao.persist.mapper.AddressMapper;
import cn.ylz.ylmall.account.pojo.param.AddressAddnewParam;
import cn.ylz.ylmall.account.pojo.param.RoleUpdateParams;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.account.service.AddressService;
import cn.ylz.ylmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@Api(tags = "1.4. 账号管理-收获地址管理")
@RequestMapping("/v1/users/address")
@Validated
public class AddressController {
    @Autowired
    private AddressService addressService;

    @PostMapping("add-new")
    @ApiOperation("新增收获地址")
    @ApiOperationSupport(order = 1)
    public JsonResult addNew(@Valid AddressAddnewParam param, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        addressService.addNew(param,principal.getId());
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除收货地址")
    @ApiOperationSupport(order = 2)
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的地址ID值！") Long id){
        addressService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改地址")
    @ApiOperationSupport(order = 3)
    public JsonResult update(@PathVariable @Range(min = 1, message = "请提交有效的地址ID值！") Long id, @Valid AddressAddnewParam param,@ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        addressService.update(id,param,principal.getId());
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询收获地址")
    @ApiOperationSupport(order = 4)
    public JsonResult addressListByUserId(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        return JsonResult.ok(addressService.addressListByUserId(principal.getId()));
    }

    @GetMapping("/default")
    @ApiOperation("查询默认收获地址")
    @ApiOperationSupport(order = 5)
    public JsonResult addressByUserId(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal){
        return JsonResult.ok(addressService.addressByUserId(principal.getId()));
    }

}
